#include "useHead.h"
#include "symTable.h"

int SymTable::add(const string &name, int size, int stackIndex, int funcIndex)
{
	if (name.empty())
		return -1;

	if (find(name, funcIndex) != -1)
		return -1;

	Symbol temp;
	temp._name = name;
	temp._index = _vs.size();
	temp._size = size;
	temp._funcIndex = funcIndex;
	temp._stackIndex = stackIndex;
	_vs.push_back(temp);

	return temp._index;
}


int SymTable::find(const string &name, int funcIndex)
{
	for (int i = 0; i < _vs.size(); ++i)
		if (_vs[i]._funcIndex == funcIndex && _vs[i]._name == name)
			return i;

	return -1;
}	


int SymTable::get(int index,  GET_TYPE flag)
{
	if (index < 0 || index > _vs.size())
		return -1;

	switch(flag) {
		case STACKINDEX:
			return _vs[index]._stackIndex;
		case SIZE:
			return _vs[index]._size;
		default:
			return -1;
	}
}


void SymTable::dump()
{
    puts("\n+++++++++++++++++<Symbol Table Dump>++++++++++++++++++++++++\n");
	printf("Symbol Number :%d\n\n", _vs.size());
	for (int i = 0; i < _vs.size(); ++i) {
		printf("Name:           %s\n", _vs[i]._name.c_str());
		printf("    size:       %d\n", _vs[i]._size);
        printf("    index:      %d\n", _vs[i]._index);
		printf("    funcIndex:  %d\n", _vs[i]._funcIndex);
		printf("    stackIndex: %d\n\n", _vs[i]._stackIndex);
	}
    puts("\n+++++++++++++++++<Symbol Table Dump>++++++++++++++++++++++++\n");
}

